﻿/// <reference path="../jquery-1.6.4-vsdoc.js" />

var img = new Image;
img.src = 'http://localhost:30288/Images/SpriteSheets/004.png';


function Map() {
    
}

Map.Initialize = function () {
    var canvas = $('#GameCanvas');

    var canvasPosition = {
        x: canvas.offset().left,
        y: canvas.offset().top
    };

    var doubleClickThreshold = 250;  //ms
    var lastClick = 0;
    var isDragging = false;
    var isDoubleClick = false;
    var mouseIsDown = false;

    canvas.click(function (e) {
        var thisClick = new Date().getTime();
        var isDoubleClick = thisClick - lastClick < doubleClickThreshold;
        lastClick = thisClick;

        var mouse = {
                    x: e.pageX - canvasPosition.x,
                    y: e.pageY - canvasPosition.y
                }

        if (isDoubleClick == true) {
            alert('X:' + mouse.x + ' Y:' + mouse.y);
        }
    });

    canvas.mousedown(function () {
        mouseIsDown = true;
    });

    canvas.mouseup(function () {
        isDragging = false;
        mouseIsDown = false;
    });

    // Using document so you can drag outside of the canvas, use $node
    // if you cannot drag outside of the canvas
    $(document).mousemove(function () {
        if (mouseIsDown) {
            isDragging = true;
        }
    });


    //canvas.click(function (e) {
    //    var mouse = {
    //        x: e.pageX - canvasPosition.x,
    //        y: e.pageY - canvasPosition.y
    //    }
    //    alert(mouse.x);

    //});
    

    //canvas.dblclick(function (e) {

    //    alert('Show Menu');

    //});

}

function clickReporter() {
    alert('test!');
}
function mousemoveReporter() {
    alert('moved!');

}
function Tile(ID) {
    this.context = document.getElementById("GameCanvas").getContext("2d");
    this.SrcX = 0;
    this.SrcY = 0;
    this.DrawX = -45;
    this.DrawY = -45;
    this.ImageID = 0;
    if (ID > 0 && ID < 1000) { ImageID = ID; }
    this.Passable = false;
    this.TilePositionID = 0;

}

Tile.prototype.Draw = function (tileIDs) {

    for (var i = 0; i < tileIDs.length; i++) {
        var TileID = parseInt(tileIDs[i]);
        var row = Math.ceil(TileID / 13);

        var mod = ((TileID % 13) - 1) * 100;
        this.SrcX = mod;
        //alert(parseInt((parseInt(tileIDs[i]) + 1) / 13));
        //this.SrcX = (parseInt(tileIDs[i]) - 1) * 100
        //if (xCol > 1) {
        //    var Xoffset = Math.floor(xCol) * img.width;
        //    this.SrcX = this.SrcX - Xoffset;
        //}
        this.SrcY = (parseInt((parseInt(tileIDs[i]) + 1) / 13)) * 100
        this.SrcY = (row-1) * 100;

        this.DrawX = (i * 55) - 45;
        this.context.drawImage(img, this.SrcX, this.SrcY, 100, 100, this.DrawX , this.DrawY, 100, 100);
    }
}